/* 
 * Advanced Foundation Classes
 * Copyright (C) 2000/2004  Fabio Rotondo - Andrea Galimberti
 *  
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *  
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *  
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include "checkbutton.h"

static const char class_name [] = "EGUI Widget CheckButton";

static int afc_easygui_internal_checkbutton_get_template ( DynamicClass * dyn );
static int afc_easygui_internal_cb_checkbutton ( DynamicClass * dyn );

static int afc_easygui_internal_checkbutton_create_button ( EasyGUIContext * context, char * text, char * action, DynamicClass * dc, short checked, char * iconname, char * textpos );

static int afc_easygui_internal_checkbutton_create_menu ( EasyGUIContext * context, char * text, char * action, DynamicClass * dc, short checked, char * iconname, char * textpos );

// Methods
static int afc_easygui_checkbutton_method_get_value ( DynamicClass * dc );
static int afc_easygui_checkbutton_method_set_value ( DynamicClass * dc );

// Events
static void afc_easygui_checkbutton_event_toggled ( GtkWidget * w, gpointer data );


DynamicClass * afc_easygui_internal_checkbutton_new_instance ()
{
	DynamicClass * dyn = NULL;

	if ( ( dyn = afc_dynamic_class_new () ) == NULL ) {
		AFC_LOG_FAST_INFO ( AFC_ERR_NO_MEMORY, "dynamic class: checkbutton" );
		return ( NULL );
	}

	afc_dynamic_class_add_method ( dyn, "get_template", NULL,  afc_easygui_internal_checkbutton_get_template );
	afc_dynamic_class_add_method ( dyn, "open_callback", NULL,  afc_easygui_internal_cb_checkbutton );
	afc_dynamic_class_add_method ( dyn, "enabled",	NULL, 	afc_easygui_widget_method_enabled );

	afc_dynamic_class_add_method ( dyn, "get_value", NULL,  afc_easygui_checkbutton_method_get_value );
	afc_dynamic_class_add_method ( dyn, "set_value", NULL,  afc_easygui_checkbutton_method_set_value );

	return ( dyn );
}

int afc_easygui_internal_checkbutton_del_instance ( DynamicClass * dyn )
{
	return ( afc_easygui_widget_standard_free ( dyn ) );
	// if ( dyn != NULL ) afc_dynamic_class_delete ( dyn );

	// return ( AFC_ERR_NO_ERROR );
}

static int afc_easygui_internal_checkbutton_get_template ( DynamicClass * dyn )
{
	dyn->result = "TEXT ICON TEXTPOS ACTION CHECKED/S";
	return ( AFC_ERR_NO_ERROR );
}

static int afc_easygui_internal_cb_checkbutton ( DynamicClass * dyn )
{
	EasyGUI * egui	= dyn->info;
	EasyGUIContext * context = egui->curr_context;
	EasyGUIWidgetData * container;
	char * action, * text, * iconname, * textpos;
	short  checked;
	int afc_res = AFC_ERR_NO_ERROR;
	DynamicClass * dc;

	if ( context->curr_container == NULL ) return ( AFC_EASYGUI_ERR_NO_CONTAINER );

	container = context->curr_container;

	afc_easygui_widget_parse_default_params ( egui );

	checked 	= ( short ) ( int ) afc_cmd_parser_arg_get_by_name ( egui->parser, "CHECKED" );
	action		= afc_cmd_parser_arg_get_by_name ( egui->parser, "ACTION" );
	text		= afc_cmd_parser_arg_get_by_name ( egui->parser, "TEXT" );
	iconname 	= afc_cmd_parser_arg_get_by_name ( egui->parser, "ICON" );
	textpos		= afc_cmd_parser_arg_get_by_name ( egui->parser, "TEXTPOS" );


	if ( ( dc = afc_dynamic_class_master_new_instance ( egui->commands, "checkbutton" ) ) == NULL )
		return ( AFC_LOG_FAST_INFO ( AFC_ERR_NO_MEMORY, "dynclass instance" ) );

	WIDGET_SET_CONTEXT ( dc, egui->curr_context );
	
	switch ( container->type )
	{
		case AFC_EASYGUI_CONTAINER_TYPE_MENU:
			afc_res = afc_easygui_internal_checkbutton_create_menu ( context, text, action, dc, checked, iconname, textpos );
			break;

		default:
			afc_res = afc_easygui_internal_checkbutton_create_button ( context, text, action, dc, checked, iconname, textpos );
			break;
	}



	return ( afc_res );
}

static int afc_easygui_internal_checkbutton_create_button ( EasyGUIContext * context, char * text, char * action, DynamicClass * dc, short checked , char * iconname, char * text_pos )
{
	int afc_res;
	GtkWidget * w;
	GtkWidget * box;

	if ( iconname != NULL )
	{
		w = gtk_check_button_new ();
		box = afc_easygui_widget_create_label ( dc, text, iconname, afc_easygui_widget_get_horient ( context, text_pos ) );
		gtk_container_add ( GTK_CONTAINER ( w ), box );
	} else {
		if ( text != NULL )
			w = gtk_check_button_new_with_label ( text );
		else
			w = gtk_check_button_new ();
	}

	// This set is done before action binding because it raises a "toggled" event
	gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON ( w ), ( gboolean ) checked );

	/* first try to add the widget to the current container, then, if successful, set the other attributes */
	if ( ( afc_res = afc_easygui_internal_add_widget ( context, w, dc ) ) == AFC_ERR_NO_ERROR ) 
	{
		if ( ( afc_res = afc_easygui_widget_connect_signal ( dc, w, "toggled", action, NULL, (GtkSignalFunc ) afc_easygui_checkbutton_event_toggled ) ) != AFC_ERR_NO_ERROR )
			return ( afc_res );

	}

	// Set the "Checkbox type" to 0 -> a checkbutton
	afc_dynamic_class_set_var ( dc, AFC_DYNAMIC_CLASS_VAR_KIND_NUM, "egui_check_type",  ( void * ) 0 );

	return ( afc_res );
}

static int afc_easygui_internal_checkbutton_create_menu ( EasyGUIContext * context, char * text, char * action, DynamicClass * dc, short checked, char * iconname, char * text_pos )
{
	int afc_res;
	GtkWidget * w;
	GtkWidget * box;

	if ( iconname != NULL )
	{
		w = gtk_check_menu_item_new ();
		box = afc_easygui_widget_create_label ( dc, text, iconname, afc_easygui_widget_get_horient ( context, text_pos ) );
		gtk_container_add ( GTK_CONTAINER ( w ), box );
	} else {
		if ( text != NULL )
			w = gtk_check_menu_item_new_with_label ( text );
		else
			w = gtk_check_menu_item_new ();
	}

	// This set is done before action binding because it raises a "toggled" event
	gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM ( w ), ( gboolean ) checked );

	/* first try to add the widget to the current container, then, if successful, set the other attributes */
	if ( ( afc_res = afc_easygui_internal_add_widget ( context, w, dc ) ) == AFC_ERR_NO_ERROR ) 
	{
		if ( ( afc_res = afc_easygui_widget_connect_signal ( dc, w, "toggled", action, NULL, (GtkSignalFunc ) afc_easygui_checkbutton_event_toggled ) ) != AFC_ERR_NO_ERROR )
			return ( afc_res );

	}

	// Set the "Checkbox type" to 1 -> a check_menu_item
	afc_dynamic_class_set_var ( dc, AFC_DYNAMIC_CLASS_VAR_KIND_NUM, "egui_check_type",  ( void * ) 1 );

	return ( afc_res );
}

// METHODS

// Gets the toggle button value
// Result is stored in dc->result
static int afc_easygui_checkbutton_method_get_value ( DynamicClass * dc )
{
	EasyGUIWidgetData * ewidget;

	WIDGET_GET_EWIDGET ( dc, ewidget );

	// If egui_check_type == 0, the widget is a checkbutton, otherwise it is a CheckMenuItem
	if ( ( ( int ) afc_dynamic_class_get_var ( dc, "egui_check_type" ) ) == 0 )
		dc->result = ( void * ) gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON ( ewidget->gtk_handle ) );
	else
		dc->result = ( void * ) gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM ( ewidget->gtk_handle ) );

	return ( AFC_ERR_NO_ERROR );	
}

// Sets the toggle button value
static int afc_easygui_checkbutton_method_set_value ( DynamicClass * dc )
{
	EasyGUIWidgetData * ewidget;
	gboolean active;

	WIDGET_GET_EWIDGET ( dc, ewidget );
	
	active = ( gboolean ) ( int ) afc_array_master_first ( dc->args );

	// If egui_check_type == 0, the widget is a checkbutton, otherwise it is a CheckMenuItem
	if ( ( ( int ) afc_dynamic_class_get_var ( dc, "egui_check_type" ) ) == 0 )
		gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON ( ewidget->gtk_handle ), active );
	else
		gtk_check_menu_item_set_active ( GTK_CHECK_MENU_ITEM ( ewidget->gtk_handle ), active );

	return ( AFC_ERR_NO_ERROR );	
}

// EVENTS
static void afc_easygui_checkbutton_event_toggled ( GtkWidget * w, gpointer data )
{
	DynamicClass * dc = data;
	EasyGUICheckSignalFunc sig;

	WIDGET_GET_CALLBACK_CAST ( dc, "toggled", sig, EasyGUICheckSignalFunc );

	// If egui_check_type == 0, the widget is a checkbutton, otherwise it is a CheckMenuItem
	if ( ( ( int ) afc_dynamic_class_get_var ( dc, "egui_check_type" ) ) == 0 )
		sig ( dc, WIDGET_GET_CONTEXT ( dc ), gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON ( w ) ) );
	else
		sig ( dc, WIDGET_GET_CONTEXT ( dc ), gtk_check_menu_item_get_active ( GTK_CHECK_MENU_ITEM ( w ) ) );
}
